import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Add database type dictionary data
 */
public class AddDictData {

    private static final String URL = "jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";
    private static final String DICT_TYPE = "db_type";

    public static void main(String[] args) {
        Connection conn = null;
        try {
            // Load driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            // Get connection
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            conn.setAutoCommit(false);
            
            // Check if dictionary type exists
            boolean dictTypeExists = checkDictTypeExists(conn);
            if (!dictTypeExists) {
                // Add dictionary type
                String insertDictTypeSql = "INSERT INTO sys_dict_type (dict_name, dict_type, status, create_by, create_time, remark) VALUES (?, ?, ?, ?, ?, ?)";
                PreparedStatement psDictType = conn.prepareStatement(insertDictTypeSql);
                psDictType.setString(1, "数据库类型");
                psDictType.setString(2, DICT_TYPE);
                psDictType.setString(3, "0");
                psDictType.setString(4, "admin");
                psDictType.setTimestamp(5, new Timestamp(new Date().getTime()));
                psDictType.setString(6, "数据库类型列表");
                psDictType.executeUpdate();
                psDictType.close();
                System.out.println("Dictionary type added successfully!");
            } else {
                System.out.println("Dictionary type already exists, skipping...");
                // Clear existing dictionary data first
                clearExistingDictData(conn);
            }
            
            // Add dictionary data: relational
            addDictData(conn, 1, "关系型", "relational", "Y", "关系型数据库");
            
            // Add dictionary data: non-relational
            addDictData(conn, 2, "非关系型", "non_relational", "N", "非关系型数据库");
            
            // Add dictionary data: standalone
            addDictData(conn, 3, "单机", "standalone", "N", "单机数据库");
            
            // Add dictionary data: distributed
            addDictData(conn, 4, "分布式", "distributed", "N", "分布式数据库");
            
            // Commit transaction
            conn.commit();
            System.out.println("Dictionary data added successfully!");
            
        } catch (Exception e) {
            e.printStackTrace();
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    private static boolean checkDictTypeExists(Connection conn) throws SQLException {
        String checkSql = "SELECT COUNT(*) FROM sys_dict_type WHERE dict_type = ?";
        PreparedStatement ps = conn.prepareStatement(checkSql);
        ps.setString(1, DICT_TYPE);
        ResultSet rs = ps.executeQuery();
        rs.next();
        boolean exists = rs.getInt(1) > 0;
        rs.close();
        ps.close();
        return exists;
    }
    
    private static void clearExistingDictData(Connection conn) throws SQLException {
        String deleteSql = "DELETE FROM sys_dict_data WHERE dict_type = ?";
        PreparedStatement ps = conn.prepareStatement(deleteSql);
        ps.setString(1, DICT_TYPE);
        ps.executeUpdate();
        ps.close();
        System.out.println("Cleared existing dictionary data");
    }
    
    private static void addDictData(Connection conn, int sort, String label, String value, String isDefault, String remark) throws SQLException {
        String insertDictDataSql = "INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, is_default, status, create_by, create_time, remark) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement psDictData = conn.prepareStatement(insertDictDataSql);
        psDictData.setInt(1, sort);
        psDictData.setString(2, label);
        psDictData.setString(3, value);
        psDictData.setString(4, DICT_TYPE);
        psDictData.setString(5, isDefault);
        psDictData.setString(6, "0");
        psDictData.setString(7, "admin");
        psDictData.setTimestamp(8, new Timestamp(new Date().getTime()));
        psDictData.setString(9, remark);
        psDictData.executeUpdate();
        psDictData.close();
    }
}